<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 放置标题图片 @是设置的别名(vue-cli默认支持)-->
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <!-- 点击显示密码 -->
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native="handleLogin"
      >登录</el-button>
      <div class="tips">
        <span style="margin-right: 20px">账号:13800000002</span>
        <span> 密码: 123456</span>
      </div>

    </el-form>
  </div>
</template>

<script>
// import { loginApi } from '@/api/user.js'

export default {
  name: 'Login',
  data() {
    return {
      // 表单参数：
      loginForm: {
        // 手机号
        mobile: '13800000002',
        // 密码
        password: '123456'
      },
      loginRules: {
        mobile: [
          // required非空校验
          { required: true, message: '请输入手机号', trigger: 'blur' },
          // 2.使用这则校验手机号格式
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        password: [
          // required非空校验

          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入密码', trigger: 'blur' }]

      },
      loading: false,
      passwordType: 'password',
      // 接收401回跳地址
      redirect: undefined
    }
  },
  watch: {
    // 监听路由参数变化
    $route: {
      // 路由参数变化执行
      handler: function(route) {
        // 注意改参数名：router.query.backUrl
        this.redirect = route.query && route.query.backUrl
      },
      // 页面加载立即执行一次监听
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        // 空字符串=>默认值 type='text'
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      //
      //
      /**
       * 1.兜底校验this.$refs.loginForm.validate
       * 2.兜底校验通过后，调用后台登录接口，传入参数()
       * 3.获取到token，存到vuex
       * 4.跳转到首页
       */

      this.$refs.loginForm.validate(async valid => {
        console.log('兜底校验', valid)
        if (valid) {
          // 写法一
          // const token = await loginApi(this.loginForm)
          // console.log('获取token', token)
          // // 因为用了模块化加锁，所以不能写第一种，要写第二种
          // // 1.语法：this.$store.commit("要触发的mutations里的函数名"，具体值)
          // // 2.this.$store.commit('模块名', mutations方法名,具体值)
          // this.$store.commit('user/setToken', token)
          // this.loading = true
          // this.$store
          //   .dispatch('user/login', this.loginForm)
          //   .then(() => {
          //     this.$router.push({ path: this.redirect || '/' })
          //     this.loading = false
          //   })
          //   .catch(() => {
          //     this.loading = false
          //   })
          // 写法2
          // 调用vuex的action：this.$store.dispatch('模块名/actions方法名'，)
          await this.$store.dispatch('user/loginAction', this.loginForm)

          this.$router.push(this.redirect || '/')
          // 记得加个await，因为action是异步的
          // 说明：this.redirect
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #ebf2f9;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url("~@/assets/common/login.jpg"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    /* reset element-ui css */
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.4);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
